<?php
/**
 * 道具管理板块
 */
require_once 'basic.inc.php';
if($loged != 1){
	echo '<script language="javascript">';
	echo 'parent.location.href="index.php?act=login";';
	echo '</script>';
	exit;
}
$title = '';
$body = '';
$tpl = '';
$propDao = new Kit_Dao_Prop();
switch($act){
	//添加页面
	case 'add':
	    $title = '添加道具';
		$tpl .= 'tpl/prop_add.tpl';
		break;
	//添加数据处理
	case 'added':
	    if(!$_REQUEST['sName']){
	        echo urlReload('prop.php?act=add', '内容不能为空');
		    exit;
	    }
		$fileName = $_FILES['sUrl']['name'];
		$_FILES['sUrl']['name'] = iconv("utf-8", "gb2312", $_FILES['sUrl']['name']);
		//上传素材图片并写入数据
		if(file_exists(PROP_URL.$_FILES['sUrl']['name'])){
		    echo urlReload('prop.php?act=add', '上传图片失败');
		    exit;
		}else{
		    if(move_uploaded_file($_FILES['sUrl']['tmp_name'], PROP_URL.$_FILES['sUrl']['name'])){
		        $data = $_REQUEST;
		        unset($data['act']);
		        unset($data['PHPSESSID']);
        	    $data['nCrTime'] = time();
        	    $data['nOnShelf'] = 1;
		        $data['sUrl'] = $_FILES['sUrl']['name'];
		        if($propDao->add($data)){
		            echo urlReload('prop.php?act=list', '添加道具成功');
		            exit;
		        }else{
		            echo urlReload('prop.php?act=list', '添加道具失败');
		            exit;
		        }
		    }
		}
		break;
	case 'list':
		$title = '道具列表';
		$prop_list = $propDao->getItems('id>0', 'id DESC', 1, 10);
		$tpl .= 'tpl/prop_list.tpl';
		break;
	case 'edit':
	    $title = '编辑道具';
	    $prop_edit = $propDao->getItem('id='.$_REQUEST['id'], '');
	    $tpl .= 'tpl/prop_edit.tpl';
	    break;
	case 'edited':
	    //如果更新图片
	    if($_FILES['sUrl']['name']){
	        $fileName = $_FILES['sUrl']['name'];
    		$fileName = iconv("utf-8", "gb2312", $fileName);
    		//上传素材图片并更新数据
    		if(file_exists(PROP_URL.$fileName)){
    		    echo urlReload('prop.php?act=list', '图片已存在');
    		    exit;
    		}else{
    		    if(move_uploaded_file($_FILES['sUrl']['tmp_name'], PROP_URL.$fileName)){
    		        $data = $_REQUEST;
    		        unset($data['act']);
    		        unset($data['PHPSESSID']);
    		        $data['sUrl'] = $_FILES['sUrl']['name'];
    		        if($propDao->update('id='.$data["id"], $data)){
    		            echo urlReload('prop.php?act=list', '编辑道具成功');
    		            exit;
    		        }else{
    		            echo urlReload('prop.php?act=list', '编辑道具失败');
    		            exit;
    		        }
    		    }
    		}
	    }else{//没有更新图片
	        $data = $_REQUEST;
	        unset($data['act']);
	        unset($data['PHPSESSID']);
	        unset($data['sUrl']);
	        if($propDao->update('id='.$data["id"], $data)){
	            echo urlReload('prop.php?act=list', '编辑道具成功');
	            exit;
	        }else{
	            echo urlReload('prop.php?act=list', '编辑道具失败');
	            exit;
	        }
	    }
		break;
	case 'delete':
	    if($_REQUEST['id'] > 0){
	        if($propDao->delete('id='.$_REQUEST['id'])){
	            echo urlReload('prop.php?act=list', '道具删除成功');
    		    exit;
	        }else{
	            echo urlReload('prop.php?act=list', '道具删除失败');
    		    exit;
	        }
	    }
	    break;
	    
	case 'static':
	    $title = '道具统计';
	    $tpl .= 'tpl/prop_static.tpl';
	    break;
	default:
	    break;
}
require 'tpl/right.phtml';

function urlReload($url, $alert){
    $return = '<script language="javascript">';
    $return .= 'alert("'.$alert.'");';
    $return .= 'window.location.href="'.$url.'";';
    $return .= '</script>';
    return $return;
}